﻿using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChuanYe.CountyFireManagement.BLL
{
    public class FireInformBLL
    {
        #region 构造单例
        private FireInformBLL() { }
        private static FireInformBLL _instance;
        public static FireInformBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new FireInformBLL());
            }
        }
        #endregion

        public ResultDto<List<FireMQTTResponseModel>> Add(List<Fireinform> infos)
        {
            ResultDto<List<FireMQTTResponseModel>> result = new ResultDto<List<FireMQTTResponseModel>>();
            var fireInfo = BLL.FireInfoBLL.Instance.Get(infos.FirstOrDefault().FireId);
            if (fireInfo == null)
            {
                result.Status = -1;
                result.Message = "FireInfo Is Not Exist";
            }
            var lastRecordInfo = BLL.RecordBLL.Instance.GetLast(fireInfo.FireId, null);
            if (lastRecordInfo == null
                || (lastRecordInfo.FirstOrDefault().Type == (int)RecordType.火灾首报))
            {
                result.Status = -2;
                result.Message = "FireInform Status Is Not Added";
            }
            List<Message> messages = new List<Message>();
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                List<int> addFireinformIds = new List<int>();
                foreach (var item in infos)
                {
                    var exist = ef.Fireinform.Where(p => p.FireId == item.FireId && p.UserId == item.TargetUserId).FirstOrDefault();
                    if(exist!=null)
                    {
                        continue;
                    }
                    ef.Fireinform.Add(item);
                    ef.Entry<Fireinform>(item).State = EntityState.Added;

                    //消息
                    Message message = new Message();
                    message.FireId = fireInfo.FireId;
                    message.Type = (int)MessageType.通知周边;
                    message.Content =item.InformContent;
                    message.SendType = (int)MessageSendType.用户对用户发送;
                    message.IsRead = 0;
                    message.HandleTime = item.TransDatetime;
                    message.SendUserId = item.UserId;
                    message.TargetUserId = item.TargetUserId;
                    message.Ip = fireInfo.Ip;
                    ef.Message.Add(message);
                    ef.Entry<Message>(message).State = EntityState.Added;
                    ef.SaveChanges();
                    addFireinformIds.Add(item.InformId);
                    #region 装载消息
                    messages.Add(message);
                    #endregion

                }
                if (infos.Count > 0)
                {
                    var userInfo = ef.Userinfo.Where(p => p.UserId == infos[0].UserId).FirstOrDefault();
                    //处置记录
                    Record record = new Record();
                    record.FireId = fireInfo.FireId;
                    record.HandleTime = infos.FirstOrDefault().TransDatetime;
                    record.Type = (int)RecordType.通知周边;
                    record.UserId = infos.FirstOrDefault().UserId;
                    record.Content = string.Format(FireInfoTool.GetFireInfoRecordFormat(record.Type.Value), userInfo.RealName, fireInfo.FireName);
                    record.AddTime = infos.FirstOrDefault().TransDatetime;
                    record.Ip = infos.FirstOrDefault().Ip;
                    record.Applicant = userInfo.RealName;
                    record.CustomData = string.Join(",", addFireinformIds);
                    ef.Record.Add(record);
                    ef.Entry<Record>(record).State = EntityState.Added;
                    ef.SaveChanges();
                }
                result.FirstParam = MessageBLL.Instance.TransMessageToFireMQTTResponseModel(messages, (int)RecordType.通知周边);
            }
            return result;
        }

        public ResultDto<List<FireMQTTResponseModel>> Complete(int informId, int status)
        {
            ResultDto<List<FireMQTTResponseModel>> result = new ResultDto<List<FireMQTTResponseModel>>();
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Fireinform.Where(p => p.InformId == informId).FirstOrDefault();
                if (exist == null)
                {
                    result.Status = -2;
                    result.Message = "FireInform Is Not Exist";
                }
                exist.Status = status;
                ef.Fireinform.Add(exist);
                ef.Entry<Fireinform>(exist).State = EntityState.Modified;
                var existUser = BLL.UserBLL.Instance.GetInformUserInfo(exist.TargetUserId, exist.FireId);
                Message message = new Message();
                message.FireId = exist.FireId;
                message.Type = (int)MessageType.通知周边;
                string happenDatetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                message.Content = $"{existUser[0].RealName}({existUser[0].AddressTownName}{existUser[0].AddressVillageName}) 已确认接收";
                message.SendType = (int)MessageSendType.用户对用户发送;
                message.IsRead = 0;
                message.HandleTime = DateTime.Now;
                message.SendUserId = exist.TargetUserId;
                message.TargetUserId = exist.UserId;
                message.Ip = exist.Ip;
                ef.Message.Add(message);
                ef.Entry<Message>(message).State = EntityState.Added;
                ef.SaveChanges();
                #region 装载消息
                List<Message> messages = new List<Message>() { message };
                result.FirstParam = MessageBLL.Instance.TransMessageToFireMQTTResponseModel(messages, (int)RecordType.通知周边);
                #endregion
                return result;
            }
        }
    }
}
